home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / natcomp.zip / COMPILE.CB next >
Text File  |  1990-03-14  |  16KB  |  561 lines

  1. /*
  2.  *        BRIEF -- Basic Reconfigurable Interactive Editing Facility
  3.  *
  4.  *        Written by Dave Nanian and Michael Strickman.
  5.  *
  6.  *        1-14-90        Nat    added code to translate environment variables
  7.  *                                which may be passed in on compile command line
  8.  */
  9.  
  10. /*
  11.  *        compile.cb:
  12.  *
  13.  *        This file contains all of the standard BRIEF macros for compiling files.
  14.  */
  15.  
  16. string trans_envar (string cmdln);                    // NAT - translates out env var's
  17. string add_to_path (string path, string name);
  18. string escape_re (string original, ~string);
  19. int next_error (~int action);
  20. int cc (string pass_string, ~string legal_extension, ~int check_warnings, ~int background, ~int continuation);
  21. void _cc_complete (int ret_code, string full_name, int check_warnings, string pass_string);
  22. int cm (...);
  23. int cb (...);
  24.  
  25. extern int    _check_warnings,    //    Check for warnings in the compiler output?
  26.                 _background;        //    Run the compiler in the background?
  27.  
  28. /*
  29.  *        compile_it:
  30.  *
  31.  *        This function automatically compiles the file in the current
  32.  *    buffer.    It uses the "BC<extension>" environment variable to
  33.  *    determine what to do with any given file.  If no "BC<extension>"
  34.  *    environment variable exists for the specific file extension being
  35.  *    compiled, compile_it checks to see if it's ".c", ".m" or ".asm".    If
  36.  *    it's a macro file the "cm" macro is executed; if it's a C file, a
  37.  *    generic "cc" command is used, and if it's an ASM file, the Macro
  38.  *    Assembler is invoked.
  39.  *
  40.  *        Other compilers can be supported very easily.  Simply set a
  41.  *    "BC<extension>" environment variable to the "pass string" you
  42.  *    want to use.  For example, if you wanted to call the (fictitious)
  43.  *    UnderWare C compiler, which has two passes called "under" and "ware",
  44.  *    you'd use the command:
  45.  *
  46.  *        set bcc="under %s;ware %s"
  47.  *
  48.  *        You must place the pass string in quotes.  If you don't, compile_it
  49.  *    calls a macro named whatever is in the pass string.  So, for example,
  50.  *    if you accidently set your pass string using the command:
  51.  *
  52.  *        set bcc=under %s;ware %s
  53.  *
  54.  *        compile_it would call a macro named "under %s;ware %s".    This probably
  55.  *    isn't what you want -- but this feature can be useful if you want to
  56.  *    run some sort of custom macro for the file extension (compile_it does
  57.  *    this when compiling cm files).
  58.  *
  59.  *        Also note that DOS requires you to double any % characters that
  60.  *    appear in a batch file.  So if you were setting the BCC variable in
  61.  *    your autoexec, you would use the line:
  62.  *
  63.  *        set bcc="under %%s;ware %%s"
  64.  *
  65.  *        Each pass begins with the name of the executable program that does
  66.  *    that compilation pass.    That is followed by the a space, the special
  67.  *    string "%s", which is replaced by the filename (with NO extension),
  68.  *    and the multiple pass separation character ";".  If you want to put
  69.  *    a ";" in your pass string, use "\;".
  70.  *
  71.  *        These special characters are very important -- don't forget them!
  72.  *    Remember that the special "%s" string is only replaced by the filename,
  73.  *    not the filename and the extension.  Up to seven of these can be specified
  74.  *    in any given pass.  If your compiler requires the extension as well,
  75.  *    place it after this string (e.g. "cc -c %s.c").
  76.  *
  77.  *        If you want to pass options to your compiler, you can place them
  78.  *    either before or after the "%s".  Placing them before puts the option
  79.  *    before the filename, and vice versa.
  80.  *
  81.  *        If your compiler doesn't return an error code, put an exclamation
  82.  *    point in front of the first pass string (either inside or outside the
  83.  *    quotes); this will override the current warnings_only setting and
  84.  *    automatically check for errors in the compiler output.
  85.  */
  86.             
  87. int compile_it (void)
  88. {
  89.     string    extension,        //    File extension of the current file.
  90.                 command;            //    Command to be used to compile file.
  91.  
  92.     inq_names (NULL, extension);
  93.     command = trans_envar( inq_environment( "BC" + upper (extension) ) );
  94.     if( !get_parm( 0, command, "Compile with: ", 80, command ) )
  95.         return( 0 );
  96.  
  97.     if (command != "")
  98.     {
  99.         if (index (command, "\""))
  100.         {
  101.             int    loc,
  102.                     background;
  103.  
  104.             background = _background;
  105.  
  106.             while (loc = index (command, "\""))
  107.                 command = substr (command, 1, loc - 1) + substr (command, loc + 1);
  108.  
  109.             if (substr (command, strlen (command), 1) == "&")
  110.             {
  111.                 background = 1;
  112.                 command = trim (substr (command, 1, strlen (command) - 1));
  113.             }
  114.             if ("!" == substr (command, 1, 1))
  115.                 returns (cc (substr (command, 2), extension, 1, background));
  116.             else
  117.                 returns (cc (command, extension, NULL, background));
  118.         }
  119.         else
  120.         {
  121.             int    check_warnings = _check_warnings;
  122.  
  123.             if (substr (command, 1, 1) == "!")
  124.             {
  125.                 check_warnings = 1;
  126.                 command = substr (command, 2);
  127.             }
  128.             returns (execute_macro (command, command, check_warnings));
  129.         }
  130.     }
  131.     else
  132.         switch (extension)
  133.         {
  134.             case "m":
  135.                 returns (cm (_check_warnings));
  136.  
  137.             case "asm":
  138.                 returns (cc ("masm %s\\;", extension));
  139.  
  140.             case "c":
  141.                 returns (cc ("cc -c %s.c", extension));
  142.  
  143.             case "cb":
  144.                 returns (cb (_check_warnings));
  145.  
  146.             default:
  147.             {
  148.                 error ("Can't compile: no BC%s environment variable.", upper (extension));
  149.                 returns (-1);
  150.             }
  151.         }
  152. }
  153.  
  154. /*
  155.  *        warnings_only:
  156.  *
  157.  *        This macro toggles whether or not errors are searched for when a
  158.  *    compile is done and the compiler returns "no errors".  Note that its
  159.  *    value is saved in the state file.
  160.  */
  161.  
  162. int warnings_only (~int)
  163. {
  164.     int    ret_code,
  165.             previous_value;
  166.  
  167.     previous_value = _check_warnings;
  168.  
  169.     if (!(ret_code = get_parm (0, _check_warnings)))
  170.         _check_warnings = !_check_warnings;
  171.  
  172.     if (!ret_code || inq_called () == "")
  173.         message ("Compile warning detection %s.", _check_warnings ? "on" : "off");
  174.  
  175.     returns (previous_value);
  176. }
  177.  
  178. /*
  179.  *        bgd_compilation:
  180.  *
  181.  *        This macro toggles whether or not compilation is done in the
  182.  *    background under operating systems that support it.  Note that this
  183.  *    value is saved in the state file.
  184.  */
  185.  
  186. int bgd_compilation (~int)
  187. {
  188.     int    ret_code,
  189.             previous_value;
  190.  
  191.     previous_value = _background;
  192.  
  193.     if (!(ret_code = get_parm (0, _background)))
  194.         _background = !_background;
  195.  
  196.     if (!ret_code || inq_called () == "")
  197.         message ("Background compilation %s.", _background ? "on" : "off");
  198.  
  199.     returns (previous_value);
  200. }
  201.  
  202. /*
  203.  *        cc:
  204.  *
  205.  *        This routine compiles the file in the current buffer using the
  206.  *    passed "pass string" and the BRIEF DOS command.  It needs a lot
  207.  *    of memory to run -- either turn swapping on or start with at least
  208.  *    256K and -M20.  Of course, this may vary depending with the size
  209.  *    and memory requirements of the specific compiler you're using.
  210.  *
  211.  *        The "pass string" passed should be of the form:
  212.  *
  213.  *            pass_1 %s;pass_2 %s;...pass_n %s
  214.  *
  215.  *        The optional second parameter is an extended file type -- this is
  216.  *    used by the "cm" and "cb" macros, and to compile other types of files
  217.  *    (e.g. .asm).
  218.  *
  219.  *        If no pass string is specified, it defaults to a generic "cc"
  220.  *    command.  If no extension is specified, it defaults to "c".
  221.  */
  222.  
  223. int cc (string pass_string, ~string legal_extension, ~int, ~int, ~int continuation)
  224. {
  225.     string    file_name,            //    The name of the file we're compiling.
  226.                 path,                    //    The path of the file we're compiling.
  227.                 extension,            //    The extension of the file we're compiling.
  228.                 command_line,         //    The compile command line.
  229.                 old_path,            //    The original path we were on.
  230.                 error_file;            //    The file to put error information in.
  231.  
  232.     int    loc,                         //    Generic index place holder.
  233.             length,                     //    Generic length place holder.
  234.             ret_code = 1,            //    Return code from DOS.
  235.             buffer_id,                 //    Buffer ID of error buffer.
  236.             check_warnings,        //    Examine result of compile for errors?
  237.             background;                //    Do compilation in background?
  238.  
  239.     /*
  240.      *        We get the name of the file from inq_names and check to see
  241.      *    if it's a legal extension.
  242.      */
  243.  
  244.     if (legal_extension == "")
  245.         legal_extension = "c";
  246.  
  247.     inq_names (path, extension, file_name);
  248.  
  249.     if (extension != legal_extension)
  250.         error ("Current buffer is not a .%s file.", pass_string);
  251.     else
  252.     {
  253.         /*